#include "../server.hpp"

class EchoServer
{
private:
    TcpServer _server;

public:
    EchoServer(int port = 8080)
        : _server(port)
    {
    }
    void MessageCallBack(const PtrConn &pcn, Buffer *buf)
    {
        DBG_LOG("%s", buf->ReadPosition());
        buf->MoveReadOffset(buf->ReadAbleSize());

        std::string str = "hello world";
        pcn->Send((char *)str.c_str(), str.size());
    }

    void ServerCloseCallBack(const PtrConn &pcn)
    {
        DBG_LOG("cloase a connection");
    }

    void ConnectedCallBack(const PtrConn &pcn)
    {
        DBG_LOG("create a new Connected %p", pcn.get());
    }

    void Start()
    {
        _server.SetPoolCapacity(2);
        _server.EnableInactiveRelease(5);
        _server.SetMessageCallback(std::bind(&EchoServer::MessageCallBack, this, std::placeholders::_1, std::placeholders::_2));
        _server.SetConnectedCallback(std::bind(&EchoServer::ConnectedCallBack, this, std::placeholders::_1));
        _server.SetClosedCallback(std::bind(&EchoServer::ServerCloseCallBack, this, std::placeholders::_1));
        _server.Start();
    }
};
